<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class RegistrationForm extends CFormModel
{
	public $agree;
	public $email;
	public $fio;
	public $password;
	public $password_twice;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('email, fio, password, password_twice', 'required'),
			// agree needs to be a boolean
			array('agree', 'boolean'),
			array('password', 'length', 'max'=>100,'min'=>6),
			array('password_twice', 'compare', 'compareAttribute'=>'password','message'=>'Пароли не совпадают'),
			array('email','unique', 'className' => 'Users'),
			array('password_twice', 'safe'),
			array('email', 'email','checkMX'=>true),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'iread'				=>	Yii::t('main', 'Да, я прочитал и согласен с условиями данного соглашения'),
			'email'				=>	Yii::t('main', 'E-mail'),
			'fio'				=>	Yii::t('main', 'Полное имя'),
			'password'			=>	Yii::t('main', 'Пароль'),
			'password_twice'	=>	Yii::t('main', 'Повторить пароль'),
			'agree'				=>	Yii::t('main', 'Я согласен с Правилами использования'),
		);
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->email,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else
			return false;
	}
}
